﻿<?php include("conn.php");
      include("func/request.php");
 mysql_query('SET NAMES UTF8'); 


	class MyPrizeBean
	{
		public $result;
		public $userid;
		public $token;

		function __construct($result,$userid,$token)
		{
			$this->result  = $result;
			$this->userid  = $userid;
			$this->token  = $token;
		}
	}

    $prize = array();
	$rert30 = mysql_query("select * from user where  phone ='".$_REQUEST[phone]."'");
	$rowf = mysql_fetch_array($rert30);
	

    if(!empty($rowf)){
	  $der=200;
	}else{

	  //商户的邀请码以010开始
	  $r2 = mysql_query("select max(userid) AS total from user");
	  $rr2 = mysql_fetch_array($r2);

	  $shu = '010'.($rr2[total]+1);

	  //是否有邀请码
	  if($_REQUEST['yaoqingma']){
	  	  $type = substr($_REQUEST['yaoqingma'],0,3);
	  	  
	  	  if($type == '010') {
	          $rert31 = mysql_query("select niname,userid from user where  yaoqingma ='" . $_REQUEST['yaoqingma'] . "'");
	          $rowf = mysql_fetch_array($rert31);
	          $rowf1['role'] = '商户';
	          $rowf1['niname']  = $rowf['niname'];
	          $rowf1['user']  = $rowf['userid'];
			  $srat = mysql_query("INSERT INTO user(password,phone,yaoqingma,pw_token) VALUES ('$_REQUEST[pass]','$_REQUEST[phone]','$shu','$token')");
	  	  } else {
	          $rert31 = mysql_query("select name, shopid from shop where shop_daima ='" . $_REQUEST['yaoqingma'] . "'");
	          $rowf1 = mysql_fetch_array($rert31);
	          $rowf1['role'] = '厂商';
	          $rowf1['niname'] = $rowf1['name'];
	          $rowf1['user']  = $rowf1['shopid'];
			  $srat = mysql_query("INSERT INTO user(password,phone,yaoqingma,pw_token) VALUES ('$_REQUEST[pass]','$_REQUEST[phone]','$shu','$token')");
	  	  }

	  }else{
	  	 //echo  "INSERT INTO user(password,phone,yaoqingma,pw_token) VALUES ('$_REQUEST[pass]','$_REQUEST[phone]','$shu','$token')";exit;
		  $srat = mysql_query("INSERT INTO user(password,phone,yaoqingma,pw_token) VALUES ('$_REQUEST[pass]','$_REQUEST[phone]','$shu','$token')");
	  }
		
		$dingid = mysql_insert_id();
		$rowfs = mysql_fetch_array(mysql_query('select * from user where userid='.$dingid));
		$url = "http://www.ewangpf.com/api/rongcloud/API/haha.php?uid=uid-".$rowfs[userid]."&uname=".$rowfs[niname]."&headimg=".$rowfs[images];
		$url_ret = file_get_contents($url);
		$tokenObj = json_decode($url_ret);
		$token = $tokenObj->token;
		mysql_query('update user set pw_token="'.$token.'"'." where  userid =".$dingid);
		$der = 100;

		if($rowf1 != false){
			$sql = "INSERT INTO yaoqing(userid,touserid,niname,role) VALUES ('" . $rowf1['user'] .  "' ,'" . $dingid . "','" . $rowf1['niname'] . "','" . $rowf1['role'] . "')";
			$srat = mysql_query($sql);
		}

		
		//$conn=mysql_connect('localhost','root','');
		mysql_select_db('xzsh');
		// 向e网生活的商家添加数据
		$sql = "INSERT INTO shop (phone,password,xzpf_userid,pw_token) VALUES ('" . $_REQUEST['phone'] . "','" . $_REQUEST['pass'] . "','" . $dingid . "','" . $token . "')";
		$query = mysql_query($sql);
		$xzsh_shopid = mysql_insert_id();
		// 向e网生活的商家添加数据 end

		mysql_select_db('xzpf');
		// 向e网生活的商家添加数据
		$query = mysql_query("UPDATE user set xzsh_shopid = " . $xzsh_shopid . " where userid=" . $dingid);
		// 向e网生活的商家添加数据 end

}
	$prize[] = new MyPrizeBean($der,$dingid,$token);

	echo json_encode($prize);
?>